package com.zshy.base.sms.factory;

import com.zshy.base.core.utils.JsonKit;
import com.zshy.base.sms.contract.enums.SmsErrorEnum;
import com.zshy.base.sms.contract.enums.SmsTypeEnum;
import com.zshy.base.sms.contract.exception.SmsException;
import com.zshy.base.sms.properties.SmsProperties;
import com.zshy.base.sms.service.SmsTypeOperator;
import com.zshy.base.sms.service.SmsTypeOperatorRouter;
import com.zshy.base.sms.service.impl.AliyunSmsTypeOperator;
import com.zshy.base.sms.service.impl.BoshitongSmsTypeOperator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

/**
 * 短信工厂
 *
 * @author zhouhengzhe
 * @version 1.0
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class SmsFactory {

    private final SmsTypeOperatorRouter smsTypeOperatorRouter;

    private final SmsProperties smsProperties;

    /**
     * 生产短信发送方
     *
     * @param provider 短信类型，
     * @return 短信发送方
     * @see SmsTypeEnum
     */
    public SmsTypeOperator buildSmsClient(String provider) {
        log.info("init构建【{}】的短信发送方,配置信息为{}", provider, JsonKit.toJson(smsProperties));
        SmsTypeOperator route = smsTypeOperatorRouter.route(provider);
        if (Objects.isNull(route)) {
            log.error("当前的【{}】的对应的yml配置为空，或者【{}】类型不存在", provider, provider);
            throw new SmsException(SmsErrorEnum.CREATE_SMS_FACTORY_ERROR);
        }
        route.init(smsProperties);
        return route;
    }

    /**
     * 获取阿里云短信发送
     */
    public AliyunSmsTypeOperator getAliyunSmsClient() {
        return (AliyunSmsTypeOperator) buildSmsClient(SmsTypeEnum.ALIYUN.getCode());
    }

    /**
     * 获取博士通短信发送
     */
    public BoshitongSmsTypeOperator getBoshitongSmsClient() {
        return (BoshitongSmsTypeOperator) buildSmsClient(SmsTypeEnum.BOSHITONG.getCode());
    }
}
